package com.atguigu.blockingqueue;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: BlockingQueueDemo2
 * Package: com.atguigu.blockingqueue
 * Description:
 *
 * @author: fu-wang
 * @create: 2024/10/29 21:28
 * version: 1.0
 */
public class BlockingQueueDemo2 {
    public static void main(String[] args) {
        //创建有界阻塞队列
        ArrayBlockingQueue<Object> queue = new ArrayBlockingQueue<>(3);
        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    queue.put(i);
                    System.out.println(Thread.currentThread().getName()+"\t"+"生产第" + i + "个馒头，" + "目前还剩" + queue.size() + "个馒头");
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"生产者").start();

        new Thread(()->{
            for (int i = 1; i <= 10; i++) {
                try {
                    System.out.println(Thread.currentThread().getName()+"\t"+"消费第" + queue.take() + "个馒头" + "目前还剩" + queue.size() + "个馒头");
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        },"消费者").start();
    }
}
